package com.attendance.www.hr.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.attendance.www.hr.beans.NcAttendanceBean;
import com.attendance.www.hr.beans.RequestApprovalSaveBean;
import com.attendance.www.hr.beans.InterfaceBean;
import com.attendance.www.hr.beans.InterfaceNcBean;
import com.attendance.www.hr.beans.RequestBean;
import com.attendance.www.hr.beans.RetroactiveApprovalBean;
import com.attendance.www.hr.dao.mapper.ApplyTravelLogMapper;
import com.attendance.www.hr.dao.mapper.ApprovalRecordMapper;
import com.attendance.www.hr.dao.mapper.AttendanceMapper;
import com.attendance.www.hr.dao.mapper.DictionaryMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.HolidayMapper;
import com.attendance.www.hr.dao.mapper.ReceiveApprovaLogMapper;
import com.attendance.www.hr.dao.mapper.RequestMapper;
import com.attendance.www.hr.dao.mapper.RequestReportMapper;
import com.attendance.www.hr.dao.mapper.RetroactiveMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.dao.mapper.TaskMapper;
import com.attendance.www.hr.entitys.ApplyTravelLog;
import com.attendance.www.hr.entitys.ApprovalRecord;
import com.attendance.www.hr.entitys.ApprovalRecordExample;
import com.attendance.www.hr.entitys.Attendance;
import com.attendance.www.hr.entitys.AttendanceExample;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.EmployeeExample;
import com.attendance.www.hr.entitys.EmployeeSchedul;
import com.attendance.www.hr.entitys.EmployeeSchedulExample;
import com.attendance.www.hr.entitys.Holiday;
import com.attendance.www.hr.entitys.HolidayExample;
import com.attendance.www.hr.entitys.ReceiveApprovaLog;
import com.attendance.www.hr.entitys.Request;
import com.attendance.www.hr.entitys.RequestExample;
import com.attendance.www.hr.entitys.RequestReport;
import com.attendance.www.hr.entitys.RequestReportExample;
import com.attendance.www.hr.entitys.Retroactive;
import com.attendance.www.hr.entitys.RetroactiveExample;
import com.attendance.www.hr.entitys.SchedulDayExample;
import com.attendance.www.hr.entitys.SchedulDayKey;
import com.attendance.www.hr.entitys.Task;
import com.attendance.www.hr.entitys.TaskExample;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.IAttendanceService;
import com.attendance.www.hr.service.IRequestService;
import com.attendance.www.hr.service.IinterfaceService;
import com.attendance.www.hr.utils.TimeUtils;
import com.attendance.www.hr.utils.ToolUtils;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 接收审批业务层
 * 
 * @author 侯
 *
 */
@Service
public class InterfaceServiceImpl implements IinterfaceService {

	@Resource
	private RequestMapper requestDao;
	@Resource
	private RetroactiveMapper retroactiveDao;
	@Resource
	private ApprovalRecordMapper approvalRecordDao;
	@Resource
	private EmployeeMapper employeeDao;
	@Resource
	private AttendanceMapper attendanceDao;
	@Resource
	private ApplyTravelLogMapper applyTravelLogDao;
	@Resource
	private DictionaryMapper dictionaryDao;
	@Resource
	private RequestReportMapper requestReportDao;
	@Resource
	private ReceiveApprovaLogMapper receiveApprovaLogDao;
	@Resource
	private EmployeeSchedulMapper employeeSchedulDao;
	@Resource
	private SchedulDayMapper schedulDayDao;
	@Resource
	private TaskMapper taskDao;
	@Resource
	private HolidayMapper holidayDao;
	@Resource
	private IRequestService requestService;
	@Resource
	private IAttendanceService attendanceService;
	@Resource
	private IApprovalService approvalService;

	/**
	 * 处理补签表和请求表
	 * 
	 * @param requestType   申请类型
	 * @param employeeNo    员工号
	 * @param requestId     申请ID号
	 * @param approvalState 申请状态
	 * @return
	 */
	@Override
	public Map<String, Object> receiveApproval(InterfaceBean interfaceBean) {
		ReceiveApprovaLog receiveApprovaLog = new ReceiveApprovaLog();
		// 反射
		BeanUtils.copyProperties(interfaceBean, receiveApprovaLog);
		receiveApprovaLogDao.insertSelective(receiveApprovaLog);

		String employeeNo = "";
		// 验证员工号是否存在
		for (int i = 0; i < interfaceBean.getApprovalFecordList().size(); i++) {
			InterfaceBean apprInterfaceBean = interfaceBean.getApprovalFecordList().get(i);
			// 员工表
			EmployeeExample employeeExample = new EmployeeExample();
			employeeExample.createCriteria().andEmployeeNoEqualTo(apprInterfaceBean.getEmployeeNo());
			List<Employee> employeeList = employeeDao.selectByExample(employeeExample);
			// 判断员工号是否存在
			if (employeeList.size() <= 0) {
				if (!StringUtils.isEmpty(employeeNo)) {
					employeeNo = employeeNo + "、";
				}
				employeeNo = employeeNo + apprInterfaceBean.getEmployeeNo();
			} else {
				apprInterfaceBean.setEmployeeId(employeeList.get(0).getEmployeeId());
			}

			// 审批状态
			if (StringUtils.isEmpty(apprInterfaceBean.getApprovalState())) {
				return ResultMap.fault("0_00006", "审批记录：状态，必填项未输入，请确认！");
			}
			// 顺序
			if (StringUtils.isEmpty(apprInterfaceBean.getSep())) {
				return ResultMap.fault("0_00007", "审批记录：顺序号，必填项未输入，请确认！");
			}
			// 日期
			if (StringUtils.isEmpty(apprInterfaceBean.getApprovalDate())) {
				return ResultMap.fault("0_00008", "审批记录：日期，必填项未输入，请确认！");
			}
		}

		if (!StringUtils.isEmpty(employeeNo)) {
			return ResultMap.fault("0_00002", "HR考勤系统员工号(" + employeeNo + ")不存在，请联系管理员!");
		}

		if (interfaceBean.getRequestType().equals("15")) {
			// 补签表
			RetroactiveExample retroactiveExample = new RetroactiveExample();
			retroactiveExample.createCriteria().andRetroactiveIdEqualTo(Integer.parseInt(interfaceBean.getRequestId()));
			List<Retroactive> retroactiveList = retroactiveDao.selectByExample(retroactiveExample);

			if (retroactiveList.size() > 0) {
				Retroactive retroactive = retroactiveList.get(0);
				
				//查询考勤日期
				Attendance attendance=attendanceDao.selectByPrimaryKey(retroactive.getAttendanceId());

				// 验证是否审批过
				if (retroactive.getApprovalState() == 2 || retroactive.getApprovalState() == 3) {
					// 错误信息提醒
					return ResultMap.fault("0_00005", "已审批!");
				}

				RequestBean requestBean=new RequestBean();
				requestBean.setStartDate(attendance.getAttendanceDate());
				// 验证是否封账
				if (requestService.verifySaveRequestClosed(requestBean)) {// 先封账验证
					return ResultMap.fault("0_00009", "该月已封账，不能审批!");
				}
				
				// 处理审批记录表信息方法
				this.updateByApprovalRecord(interfaceBean.getApprovalFecordList(), null, retroactive.getRetroactiveId(),
						interfaceBean);

			} else {
				// 错误信息提醒
				return ResultMap.fault("0_00003", "未查询到补签申请，请联系管理员!");
			}
		} else {
			// 请求表
			RequestExample requestExample = new RequestExample();
			requestExample.createCriteria().andRequestIdEqualTo(Integer.parseInt(interfaceBean.getRequestId()));
			List<Request> requestList = requestDao.selectByExample(requestExample);

			if (requestList.size() > 0) {
				Request request = requestList.get(0);

				// 验证是否审批过
				if (request.getApprovalState() == 2 || request.getApprovalState() == 3) {
					// 错误信息提醒
					return ResultMap.fault("0_00005", "已审批!");
				}
				
				RequestBean requestBean=new RequestBean();
				requestBean.setStartDate(request.getStartDate());
				// 验证是否封账
				if (requestService.verifySaveRequestClosed(requestBean)) {// 先封账验证
					return ResultMap.fault("0_00009", "该月已封账，不能审批!");
				}

				// 处理审批记录表信息方法
				this.updateByApprovalRecord(interfaceBean.getApprovalFecordList(), request.getRequestId(), null,
						interfaceBean);

			} else {
				// 错误信息提醒
				return ResultMap.fault("0_00004", "未查询到休假申请，请联系管理员!");
			}
		}
		return ResultMap.success(true);
	}

	/**
	 * 处理查询某个人考勤信息接口
	 */
	@Override
	public Object getAttendanceDateByEmployeeNo(InterfaceNcBean interfaceNcBean) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
		NcAttendanceBean interfaceAttendanceDateBean = new NcAttendanceBean();
		// 员工表
		EmployeeExample employeeExample = new EmployeeExample();
		employeeExample.createCriteria().andEmployeeNoEqualTo(interfaceNcBean.getEmployeeNo());
		List<Employee> employeeList = employeeDao.selectByExample(employeeExample);

		List<SchedulDayKey> schedulDayList = null;
		List<Holiday> holidayList = null;

		// 判断员工号是否存在
		if (employeeList.size() <= 0) {
			return ResultMap.fault("0_00002", "HR考勤系统员工号不存在，请联系管理员!");
		} else {
			AttendanceExample attendanceExample = new AttendanceExample();
			try {
				Integer employeeId = employeeList.get(0).getEmployeeId();
				Date attendanceDate = sdf.parse(interfaceNcBean.getAttendanceDate());
				String year = sdfYear.format(attendanceDate);

				attendanceExample.createCriteria().andEmployeeIdEqualTo(employeeId)
						.andAttendanceDateEqualTo(attendanceDate);

				// 取得人员的排班时间
				EmployeeSchedulExample employeeSchedulExample = new EmployeeSchedulExample();
				employeeSchedulExample.or().andEmployeeIdEqualTo(employeeId)
						.andFromDateLessThanOrEqualTo(attendanceDate).andThruDateGreaterThanOrEqualTo(attendanceDate);
				employeeSchedulExample.or().andEmployeeIdEqualTo(employeeId)
						.andFromDateLessThanOrEqualTo(attendanceDate).andThruDateIsNull();
				employeeSchedulExample.setOrderByClause("from_date desc");
				List<EmployeeSchedul> employeeSchedulList = employeeSchedulDao.selectByExample(employeeSchedulExample);
				if (employeeSchedulList == null || employeeSchedulList.size() == 0) {
					// 未找到排班信息
					return ResultMap.fault("0_00003", "该员工未设置排班!");
				}

				// 判断是否是休息日
				SchedulDayExample sdExample = new SchedulDayExample();
				sdExample.createCriteria().andSdDateEqualTo(attendanceDate)
						.andSwIdEqualTo(employeeSchedulList.get(0).getSwId());
				schedulDayList = schedulDayDao.selectByExample(sdExample);

				// 查询法定节假日
				HolidayExample holidayExample = new HolidayExample();
				holidayExample.createCriteria().andHolidayYearEqualTo(Integer.parseInt(year))
						.andHolidayDateEqualTo(attendanceDate);
				holidayList = holidayDao.selectByExample(holidayExample);

			} catch (ParseException e) {
				e.printStackTrace();
			}

			List<Attendance> attendanceList = attendanceDao.selectByExample(attendanceExample);
			if (attendanceList.size() > 0) {
				Attendance attendance = attendanceList.get(0);
				// 反射
				BeanUtils.copyProperties(attendance, interfaceAttendanceDateBean);
				interfaceAttendanceDateBean.setState(1);
			}
			
			interfaceAttendanceDateBean.setIsVisaWaiver(employeeList.get(0).getIsVisaWaiver());

			// 如果没查到数据，证明是休息日加班。
			if (schedulDayList == null || schedulDayList.size() == 0) {
				interfaceAttendanceDateBean.setState(2);
			}
			// 验证法定节假日
			if (holidayList.size() > 0) {
				Holiday holiday = holidayList.get(0);
				// 判断是否为法定节假日
				if (holiday.getIsLegal() != null && holiday.getIsLegal() == 1) {
					interfaceAttendanceDateBean.setState(3);
				} else {
					// 判断是否为上班日或休息日
					if (holiday.getIsToWork() == 1) {
						interfaceAttendanceDateBean.setState(1);
					} else {
						interfaceAttendanceDateBean.setState(2);
					}
				}
			}
		}
		return ResultMap.success(interfaceAttendanceDateBean);
	}

	/**
	 * 出差申请
	 * 
	 * @param interfaceBean
	 * @return
	 */
	@Override
	public Map<String, Object> applyTravel(InterfaceBean interfaceBean) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		RequestBean requestBean = new RequestBean();
		ApplyTravelLog applyTravelLog = new ApplyTravelLog();
		// 反射
		BeanUtils.copyProperties(interfaceBean, applyTravelLog);

		// 员工表
		EmployeeExample employeeExample = new EmployeeExample();
		employeeExample.createCriteria().andEmployeeNoEqualTo(interfaceBean.getEmployeeNo());
		List<Employee> employeeList = employeeDao.selectByExample(employeeExample);
		// 判断员工号是否存在
		if (employeeList.size() <= 0) {
			applyTravelLog.setType(2);
			applyTravelLogDao.insertSelective(applyTravelLog);
			return ResultMap.fault("0_00002", "HR考勤系统员工号不存在，请联系管理员!");
		}

		try {
			requestBean.setStartDate(sdf.parse(interfaceBean.getStartDate()));
			requestBean.setEmployeeId(employeeList.get(0).getEmployeeId());
			requestBean.setStartDate(sdf.parse(interfaceBean.getStartDate()));
			requestBean.setEndDate(sdf.parse(interfaceBean.getEndDate()));
			requestBean.setStartTime(interfaceBean.getStartTime());
			requestBean.setStartToTime(interfaceBean.getStartToTime());
			requestBean.setEndTime(interfaceBean.getEndTime());
			requestBean.setRequestType(8);
			requestBean.setHours(new BigDecimal(interfaceBean.getHours()));
			requestBean.setDescription(interfaceBean.getDescription());
			requestBean.setRequestDate(sdf.parse(interfaceBean.getRequestDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// 验证跨天考勤日期
		int count = attendanceService.getAttendanceCount(requestBean.getEmployeeId(), requestBean.getStartDate());
		if (count == 0 && requestBean.getStartToTime() == null) {
			return ResultMap.fault("0_00006", "因跨天或当天未有考勤，申请开始日期需要填写结束时间！");
		}

		// 验证时长
		// 小于1小时判断，60分钟
		if (employeeList.get(0).getIsVisaWaiver() == 0) {
			if (requestBean.getHours() != null && requestBean.getHours().intValue() < 60) {
			   applyTravelLog.setType(5);
			   applyTravelLogDao.insertSelective(applyTravelLog);
			   return ResultMap.fault("0_00005", "申请时长需大于1小时!");
			}
		}

		// 验证是否封账
		if (requestService.verifySaveRequestClosed(requestBean)) {// 先封账验证
			applyTravelLog.setType(3);
			applyTravelLogDao.insertSelective(applyTravelLog);
			return ResultMap.fault("0_00003", "该月份已封账不能申请!");
		}

		if (requestService.verifySaveRequest(requestBean)) {// 是否可申请验证
			applyTravelLog.setType(4);
			applyTravelLogDao.insertSelective(applyTravelLog);
			return ResultMap.fault("0_00004", "该日期范围，已存在其它申请!");
		}

		// 保存出差申请
		Request request = new Request();
		request.setEmployeeId(requestBean.getEmployeeId());
		request.setRequestType(requestBean.getRequestType());
		request.setStartDate(requestBean.getStartDate());
		request.setEndDate(requestBean.getEndDate());
		request.setStartTime(requestBean.getStartTime());
		request.setStartToTime(requestBean.getStartToTime());
		request.setEndTime(requestBean.getEndTime());
		request.setHours(requestBean.getHours());
		request.setApprovalState(1);
		request.setRequestDate(requestBean.getRequestDate());
		request.setDescription(requestBean.getDescription());
		int record = requestDao.insertSelective(request);
		if (record > 0) {
			// 添加审批记录表信息
			ApprovalRecord approvalRecord = new ApprovalRecord();
			approvalRecord.setRequestId(request.getRequestId());
			approvalRecord.setIsApprove(1);
			approvalRecordDao.insertSelective(approvalRecord);

			applyTravelLog.setType(1);
			applyTravelLogDao.insertSelective(applyTravelLog);
		}
		return ResultMap.success(request.getRequestId());
	}

	/**
	 * 处理审批记录表信息方法
	 * 
	 * @param employeeId
	 * @param requestId
	 * @param retroactiveId
	 * @param interfaceBean
	 */
	private void updateByApprovalRecord(List<InterfaceBean> approvalFecordList, Integer requestId,
			Integer retroactiveId, InterfaceBean interfaceBean) {
		// 审批记录表
		if (requestId != null) {
			// 取出页面选择的申请信息
			RequestExample requestExample = new RequestExample();
			requestExample.createCriteria().andRequestIdEqualTo(requestId).andApprovalStateNotEqualTo(0);
			List<Request> requestList = requestDao.selectByExample(requestExample);

			for (Request request : requestList) {
				RequestApprovalSaveBean requestApprovalSaveBean = new RequestApprovalSaveBean();

				// 审批状态
				if (interfaceBean.getApprovalState().equals("0")) {
					requestApprovalSaveBean.setApprovalState(2);
				} else if (interfaceBean.getApprovalState().equals("1")) {
					requestApprovalSaveBean.setApprovalState(3);
				}

				// 如果审批驳回，修改审批记录和申请表状态
				if (requestApprovalSaveBean.getApprovalState() == 3) {
					// 先删除审批记录
					ApprovalRecordExample approvalRecordExample = new ApprovalRecordExample();
					approvalRecordExample.createCriteria().andRequestIdEqualTo(requestId);
					approvalRecordDao.deleteByExample(approvalRecordExample);

					// 遍历审批记录集合
					for (int j = 0; j < approvalFecordList.size(); j++) {
						InterfaceBean apprInterfaceBean = approvalFecordList.get(j);
						ApprovalRecord approvalRecord = new ApprovalRecord();
						approvalRecord.setRequestId(requestId);
						approvalRecord.setEmployeeId(apprInterfaceBean.getEmployeeId());
						// 审批状态
						if (apprInterfaceBean.getApprovalState().equals("0")) {
							approvalRecord.setIsApprove(2);
						} else if (apprInterfaceBean.getApprovalState().equals("1")) {
							approvalRecord.setIsApprove(3);
						}
						approvalRecord.setSep(Integer.parseInt(apprInterfaceBean.getSep()));
						approvalRecord.setApprovalDate(ToolUtils.formatHoursData(apprInterfaceBean.getApprovalDate()));
						approvalRecord.setApprovalDescription(apprInterfaceBean.getDescription());
						approvalRecordDao.insertSelective(approvalRecord);
					}

					request.setRequestId(request.getRequestId());
					request.setApprovalState(3);
					request.setUpdateTime(null);// 设置更新时间
					requestDao.updateByPrimaryKeySelective(request);

					/*
					 * // 删除链接linkAddress approvalService.deleteLinkAdd(request.getRequestId());
					 * 
					 * Employee employee=employeeDao.selectByPrimaryKey(employeeId);
					 * DictionaryExample dicExample = new DictionaryExample();
					 * dicExample.createCriteria() .andDicClsEqualTo("4")
					 * .andDicCdEqualTo(request.getRequestType().toString()); List<Dictionary>
					 * dicList = dictionaryDao.selectByExample(dicExample);
					 * 
					 * //提醒申请人 String textPart = "您的"+
					 * dicList.get(0).getDicName()+"申请被审批人："+employee.getChineseName()+"拒绝";
					 * approvalService.sendEmailByReject(request.getEmployeeId(),
					 * textPart,requestApprovalSaveBean.getDescription());
					 */
				}
				// 审批通过
				else if (requestApprovalSaveBean.getApprovalState() == 2) {
					// 先删除审批记录
					ApprovalRecordExample approvalRecordExample = new ApprovalRecordExample();
					approvalRecordExample.createCriteria().andRequestIdEqualTo(requestId);
					approvalRecordDao.deleteByExample(approvalRecordExample);

					// 遍历审批记录集合
					for (int j = 0; j < approvalFecordList.size(); j++) {
						InterfaceBean apprInterfaceBean = approvalFecordList.get(j);
						ApprovalRecord approvalRecord = new ApprovalRecord();
						approvalRecord.setRequestId(requestId);
						approvalRecord.setEmployeeId(apprInterfaceBean.getEmployeeId());
						// 审批状态
						if (apprInterfaceBean.getApprovalState().equals("0")) {
							approvalRecord.setIsApprove(2);
						} else if (apprInterfaceBean.getApprovalState().equals("1")) {
							approvalRecord.setIsApprove(3);
						}
						approvalRecord.setSep(Integer.parseInt(apprInterfaceBean.getSep()));
						approvalRecord.setApprovalDate(ToolUtils.formatHoursData(apprInterfaceBean.getApprovalDate()));
						approvalRecord.setApprovalDescription(apprInterfaceBean.getDescription());
						approvalRecordDao.insertSelective(approvalRecord);
					}

					// 审批人是最终审批者，修改申请表状态
					request.setApprovalState(2);
					request.setUpdateTime(new Date());
					requestDao.updateByPrimaryKeySelective(request);

					// 删除链接linkAddress
					// approvalService.deleteLinkAdd(request.getRequestId());

					// update，先查询天
					RequestReportExample requestReportExample = new RequestReportExample();
					requestReportExample.createCriteria().andRequestIdEqualTo(request.getRequestId());
					List<RequestReport> rrList = requestReportDao.selectByExample(requestReportExample);

					// 查询任务表是否已有数据
					TaskExample taskExample = new TaskExample();
					taskExample.createCriteria().andEmployeeIdEqualTo(request.getEmployeeId()).andStateEqualTo(0);
					List<Task> taskList = taskDao.selectByExample(taskExample);// 查询是否已有数据
					Map<Date, Integer> taskDateMap = new HashMap<>();
					for (Task task : taskList) {
						taskDateMap.put(task.getTaskDate(), 1);
					}
					// 再循环日期，重新生成
					for (RequestReport requestReport : rrList) {
						// 插入任务表数据
						if (!taskDateMap.containsKey(requestReport.getRequestReportDate())) {// 不包含
							Task task = new Task();
							task.setEmployeeId(requestReport.getEmployeeId());
							task.setState(0);
							task.setTaskDate(requestReport.getRequestReportDate());
							taskDao.insertSelective(task);
						}
					}

					// 更新余额表和考勤状态
					approvalService.updateAfterApproval(request);
				}
			}
		}

		// 审批补签
		if (retroactiveId != null) {
			RetroactiveApprovalBean retroactiveApprovalBean = new RetroactiveApprovalBean();

			// 审批状态
			if (interfaceBean.getApprovalState().equals("0")) {
				retroactiveApprovalBean.setApprovalState(2);
			} else if (interfaceBean.getApprovalState().equals("1")) {
				retroactiveApprovalBean.setApprovalState(3);
			}

			// 取出页面选择的补签信息
			List<Integer> retroactiveIdList = new ArrayList<Integer>();
			retroactiveIdList.add(retroactiveId);

			RetroactiveExample rExample = new RetroactiveExample();
			rExample.createCriteria().andRetroactiveIdIn(retroactiveIdList);
			List<Retroactive> retroactiveList = retroactiveDao.selectByExample(rExample);

			for (Retroactive retroactive : retroactiveList) {
				// 更新考勤记录
				Attendance attendance = attendanceDao.selectByPrimaryKey(retroactive.getAttendanceId());

				// 如果审批驳回，修改审批表和补签表状态，还有考勤表
				if (retroactiveApprovalBean.getApprovalState() == 3) {
					// 先删除审批记录
					ApprovalRecordExample approvalRecordExample = new ApprovalRecordExample();
					approvalRecordExample.createCriteria().andRetroactiveIdEqualTo(retroactiveId);
					approvalRecordDao.deleteByExample(approvalRecordExample);

					// 遍历审批记录集合
					for (int j = 0; j < approvalFecordList.size(); j++) {
						InterfaceBean apprInterfaceBean = approvalFecordList.get(j);
						ApprovalRecord approvalRecord = new ApprovalRecord();
						approvalRecord.setRetroactiveId(retroactiveId);
						approvalRecord.setEmployeeId(apprInterfaceBean.getEmployeeId());
						// 审批状态
						if (apprInterfaceBean.getApprovalState().equals("0")) {
							approvalRecord.setIsApprove(2);
						} else if (apprInterfaceBean.getApprovalState().equals("1")) {
							approvalRecord.setIsApprove(3);
						}
						approvalRecord.setSep(Integer.parseInt(apprInterfaceBean.getSep()));
						approvalRecord.setApprovalDate(ToolUtils.formatHoursData(apprInterfaceBean.getApprovalDate()));
						approvalRecord.setApprovalDescription(apprInterfaceBean.getDescription());
						approvalRecordDao.insertSelective(approvalRecord);
					}

					retroactive.setApprovalState(retroactiveApprovalBean.getApprovalState());
					retroactive.setUpdateTime(new Date());
					retroactiveDao.updateByPrimaryKeySelective(retroactive);

					// 修改考勤记录 --zx
					Attendance attendances = new Attendance();
					attendances.setAttendanceId(retroactive.getAttendanceId());
					attendances.setIsRepaired(3);// 补签失败
					attendances.setIsApprovaled(3);// 审批拒绝
					attendanceDao.updateByPrimaryKeySelective(attendances);

					// 删除链接linkAddress
					// LinkAddressExample laExample = new LinkAddressExample();
					// laExample.createCriteria().andRetroactiveIdEqualTo(retroactive.getRetroactiveId());
					// linkAddressDao.deleteByExample(laExample);

					// Employee employee=employeeDao.selectByPrimaryKey(employeeId);
					// 提醒申请人
					// String textPart = "您的补签申请被拒绝<br>审批人："+employee.getChineseName();
					// 邮件提醒
					// this.sendEmailByReject(retroactive.getEmployeeId(),textPart,retroactiveApprovalBean.getDescription());
				}

				// 审核通过
				if (retroactiveApprovalBean.getApprovalState() == 2) {
					// 先删除审批记录
					ApprovalRecordExample approvalRecordExample = new ApprovalRecordExample();
					approvalRecordExample.createCriteria().andRetroactiveIdEqualTo(retroactiveId);
					approvalRecordDao.deleteByExample(approvalRecordExample);

					// 遍历审批记录集合
					for (int j = 0; j < approvalFecordList.size(); j++) {
						InterfaceBean apprInterfaceBean = approvalFecordList.get(j);
						ApprovalRecord approvalRecord = new ApprovalRecord();
						approvalRecord.setRetroactiveId(retroactiveId);
						approvalRecord.setEmployeeId(apprInterfaceBean.getEmployeeId());
						// 审批状态
						if (apprInterfaceBean.getApprovalState().equals("0")) {
							approvalRecord.setIsApprove(2);
						} else if (apprInterfaceBean.getApprovalState().equals("1")) {
							approvalRecord.setIsApprove(3);
						}
						approvalRecord.setSep(Integer.parseInt(apprInterfaceBean.getSep()));
						approvalRecord.setApprovalDate(ToolUtils.formatHoursData(apprInterfaceBean.getApprovalDate()));
						approvalRecord.setApprovalDescription(apprInterfaceBean.getDescription());
						approvalRecordDao.insertSelective(approvalRecord);
					}

					// 审批人是最终审批者，修改补签表状态
					retroactive.setApprovalState(retroactiveApprovalBean.getApprovalState());
					retroactive.setUpdateTime(new Date());
					retroactiveDao.updateByPrimaryKeySelective(retroactive);

					// 查看还有没有其他审批中的补签申请
					RetroactiveExample reExample = new RetroactiveExample();
					reExample.createCriteria().andAttendanceIdEqualTo(attendance.getAttendanceId())
							.andApprovalStateEqualTo(1);
					List<Retroactive> reList = retroactiveDao.selectByExample(reExample);
					if (reList != null && reList.size() > 0) {
						attendance.setIsApprovaled(1);// 审批中
						attendance.setIsRepaired(1);
					} else {
						attendance.setIsApprovaled(2);// 审批通过
						attendance.setIsRepaired(2);
					}
					if (StringUtils.isEmpty(attendance.getStartTime())
							&& !StringUtils.isEmpty(retroactive.getStartTime())) {
						attendance.setStartTime(retroactive.getStartTime());
					}
					if (!StringUtils.isEmpty(attendance.getStartTime())
							&& !StringUtils.isEmpty(retroactive.getStartTime())) {
						if (TimeUtils.compareTime(attendance.getStartTime(), retroactive.getStartTime()) > 0) {
							attendance.setStartTime(retroactive.getStartTime());
						}
					}
					if (StringUtils.isEmpty(attendance.getEndTime())
							&& !StringUtils.isEmpty(retroactive.getEndTime())) {
						attendance.setEndTime(retroactive.getEndTime());
					}
					if (!StringUtils.isEmpty(attendance.getEndTime())
							&& !StringUtils.isEmpty(retroactive.getEndTime())) {
						if (TimeUtils.compareTime(attendance.getEndTime(), retroactive.getEndTime()) < 0) {
							attendance.setEndTime(retroactive.getEndTime());
						}
					}
					attendanceDao.updateByPrimaryKeySelective(attendance);

					// 删除链接linkAddress
					// LinkAddressExample laExample = new LinkAddressExample();
					// laExample.createCriteria().andRetroactiveIdEqualTo(retroactive.getRetroactiveId());
					// linkAddressDao.deleteByExample(laExample);

					// 插入任务表数据
					// this.insertTaskByParam(attendance.getAttendanceDate(),
					// attendance.getEmployeeId());
				}

				List<Integer> employeeList = new ArrayList<Integer>();
				employeeList.add(retroactive.getEmployeeId());
				approvalService.resetAttendance(attendance.getAttendanceDate(), attendance.getAttendanceDate(),
						employeeList);
			}
		}
	}
}
